<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 视图卡片 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCate">
            添加分类
          </el-button>
        </el-col>
      </el-row>
      <tree-table
        class="treeTable"
        :data="CateList"
        :columns="columns"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
        :expand-type="false"
        :selection-type="false"
      >
        <template slot="isOK" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color:lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">标签一</el-tag>
          <el-tag
            type="success"
            size="mini"
            v-else-if="scope.row.cat_level === 1"
            >标签二</el-tag
          >
          <el-tag type="danger" size="mini" v-else>标签三</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="editCaseForm(scope.row.cat_id)"
            >操作</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteCate(scope.row.cat_id)"
            >删除</el-button
          >
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="userInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="addCateFormdialogVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <el-form
        ref="addCateFormRef"
        :model="addCateForm"
        label-width="80px"
        :rules="addCateFormRules"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader
            class="cascader"
            v-model="cateCascaderId"
            :options="ParentCateList"
            expandTrigger="hover"
            :props="cateCascaderProps"
            @change="cateCascaderChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateFormdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateCause">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="操作分类"
      :visible.sync="editCasedialogVisible"
      width="50%"
      @close="editCaseFormClosed"
    >
      <el-form
        ref="editCaseFormRef"
        :model="editCaseFormKeys"
        label-width="80px"
        :rules="editCaseFormRules"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editCaseFormKeys.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCasedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCaseClicked">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      CateList: [],
      userInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isOK'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCateFormdialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: '0',
        cat_level: '0'
      },
      addCateFormRules: {
        cat_name: { required: true, message: '请输入分类名称', trigger: 'blur' }
      },
      ParentCateList: [],
      cateCascaderId: [],
      cateCascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      editCasedialogVisible:false,
      editCaseFormKeys:{

      },
      editCaseFormRules:{
        cat_name:{ required: true, message: '请输入分类名称', trigger: 'blur' }
      },
      editId:''
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.userInfo
      })
      //   console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      // console.log(res.data);
      this.CateList = res.data.result
      // console.log(this.CateList);
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.userInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newNum) {
      this.userInfo.pagenum = newNum
      this.getCateList()
    },
    addCate() {
      this.getParentCateList()
      this.addCateFormdialogVisible = true
    },
    async getParentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      //   console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级元素失败')
      }
      this.ParentCateList = res.data
    },
    cateCascaderChange() {
      // console.log(this.cateCascaderId);
      if (this.cateCascaderId.length > 0) {
        this.addCateForm.cat_pid = this.cateCascaderId[
          this.cateCascaderId.length - 1
        ]
        this.addCateForm.cat_level = this.cateCascaderId.length
        return
      } else {
        this.addCateForm.cat_pid = '0'
        this.addCateForm.cat_level = '0'
      }
    },
    addCateCause(){
      // console.log(this.addCateForm);
      this.$refs.addCateFormRef.validate(async valid=>{
        if(!valid) return
      const {data:res}= await this.$http.post('categories',this.addCateForm)
      if(res.meta.status!==201){
        return this.$message.error('添加分类失败')
      }
      this.$message.success('添加分类成功')
      this.getCateList()
      this.addCateFormdialogVisible=false
      })
    },
    addCateDialogClosed(){
      this.$refs.addCateFormRef.resetFields()
      this.cateCascaderId=[]
      this.addCateForm.cat_level='0'
      this.addCateForm.cat_pid='0'
    },
    async editCaseForm(id){
      this.editId=id
    const{data:res} =await this.$http.get('categories/'+id)
    if(res.meta.status!==200) {
      return this.$message.error('查询分类失败')
    }
    // console.log(res.data);
    console.log(res.data);
    this.editCaseFormKeys=res.data
    this.editCasedialogVisible=true
    },
    editCaseFormClosed(){
      this.$refs.editCaseFormRef.resetFields()
    },
     editCaseClicked(){
      this.$refs.editCaseFormRef.validate(async valid=>{
        if(!valid){
          return this.$message.error('请输入修改的分类名称')
        }
        const{data:res}= await this.$http.put('categories/'+this.editId, this.editCaseFormKeys)
        if(res.meta.status!==200){
          return this.$message.error('修改失败')
        }
        this.$message.success('修改成功')
        this.getCateList()
        this.editCasedialogVisible=false
      })
    },
    async deleteCate(id){
     const confirmResult= await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(error=>error)
        if(confirmResult!=='confirm'){
          return this.$message.info('取消删除')
        }
      const{data:res}= await this.$http.delete('categories/'+id)
      if(res.meta.status!==200){
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getCateList()
    }
  }
}
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 15px;
}
.cascader {
  width: 100%;
}
</style>
